package com.vecspace.dayu.code.dao;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Repository;

import com.vecspace.data.model.NameItem;
import com.vecspace.database.dao.BaseDao;
import com.vecspace.dayu.code.pojo.Func;
import com.vecspace.utils.Convert;

/** 功能定义,操作数据库的dao类
 * @author: wanghua
 */
@Repository("dayu_funcDao")
public class FuncDao extends BaseDao {
	/** 根据ID查询 功能定义
	 * @param id   主键值
	 * @return Func
	 * @throws Exception
	 */
	public Func getFunc(Long id) throws Exception {
		Func func = null;
		if (id != null && id != 0) {

			func = this.queryObject(Func.class, id);
			this.evict(func);
		}
		return func;
	}
	
	/**获取系统的一级功能
	 * 
	 * @param projectId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<Func> getRootFuncs(Long projectId) throws Exception {
		List<Func> funcs = this.queryObjectList("from Func where projectId=?0 and parentId=-1 and delTag=0 order by ordnum", new Object[] {projectId});
		return funcs;
	}
	
	/**获取系统的一级功能个数
	 * 
	 * @param projectId
	 * @return 
	 * @author wanghua
	 */
	public int getRootFuncsCount(Long projectId) throws Exception {
		int rcount = this.queryInt("select count(*) from DAYU_CODE_FUNC where project_id=? and parent_id=-1 and del_tag=0", new Object[] {projectId});
		return rcount;
	}

	/**获取下一级功能
	 * 
	 * @param parentId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<Func> getChildFuncs(Long parentId) throws Exception {
		List<Func> funcs = this.queryObjectList("from Func where parentId=?0 and delTag=0 order by ordnum", new Object[] {parentId});
		return funcs;
	}
	
	public int getChildFuncCount(Long parentId) throws Exception {
		String sql = "select count(*) from DAYU_CODE_FUNC where parent_id=?";
		int count = this.queryInt(sql, new Object[] {parentId});
		return count;
	}


	/**获取系统的所有功能
	 * 
	 * @param projectId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<Func> getAllFuncs(Long projectId) throws Exception {
		List<Func> funcs = this.queryObjectList("from Func where projectId=?0 and delTag=0 order by ordnum", new Object[] {projectId});
		return funcs;
	}
	
	/**
	 * 获取项目内已有的java包名
	 * @param projectId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<String> getJavaPkgs(Long projectId) throws Exception {
		String sql = "select distinct JAVA_PKG from DAYU_CODE_FUNC where PROJECT_ID = ? order by JAVA_PKG";
		List<Object[]> rows = this.queryDataRowList(sql, new Object[] {projectId});
		List<String> javaPkgs = rows.stream().map(row ->{
			String javaPkg = (String)row[0];
			return javaPkg;
		}).collect(Collectors.toList());
		return javaPkgs;
	}
	
	/**获取所有可用的页面类型
	 * 
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<NameItem> getValidPageLayouts(String terminalType) throws Exception {
		List<Object[]> rows = this.queryDataRowList("select ID, LAYOUT_NAME from DAYU_CODE_TEMPLATE_PAGE_LAYOUT where TERMINAL_TYPE=? and STATUS_ = 1 order by ORDNUM", new Object[] { terminalType });
		List<NameItem> nameItems = rows.stream().map(row->{
			NameItem item = new NameItem();
			item.setId(row[0]);
			item.setName(Convert.toString(row[1], ""));
			return item;
		}).collect(Collectors.toList());
		return nameItems;
	}
	
	/** 插入或修改 功能定义 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 */
	public void saveFunc(Func item) throws Exception {
		this.saveOrUpdateObject(item);
		this.evict(item);
	}

	/** 根据ID删除 功能定义 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 */
	public void deleteFunc(Long id) throws Exception {
		if (id != null && id != 0) {
			this.deleteObject(Func.class, id);
		}
	}
}
